package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.common.resopnse.JsonResult;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.InsertUserDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

//@Controller
@RestController
/**
 * RestController注解:
 * 组合注解:相当于 Controller+ResponseBody注解
 */
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired(required = false)//=false表示装配不成功也不会报错
    private UserMapper userMapper;

    //@RequestMapping("insert")
//    @ResponseBody
    @PostMapping("insert")
    public String insertUser(InsertUserDTO insertUserDTO) {
        /*
        1.确定是否需要VO或DTO
        2.梳理业务逻辑,确定接口
         */
        User user = new User();
//        user.setUsername(insertUserDTO.getUsername());
        //复制属性,第一个参数的属性拿出复制到第二个参数中
        BeanUtils.copyProperties(insertUserDTO, user);
        user.setCreated(new Date());
        //调用接口
        userMapper.insertUser(user);
        return "插入成功";


    }
//    @RequestMapping("userList")
//    @ResponseBody
    @GetMapping("userList")
    public JsonResult userList(){
        return JsonResult.ok();
    }
    /**
     * 3.删除指定用户功能
     */
//    @RequestMapping("delete")
//    @ResponseBody
    @GetMapping("delete")
    public JsonResult delete(Integer id){
        userMapper.delete(id);

        return JsonResult.ok();
    }

    /**
     * 更新
     */
//    @RequestMapping("update")
//    @ResponseBody
    @PostMapping("update")
    public String update(User user){
        userMapper.update(user);
        return "更新成功";
    }

    /**
     * 5.根据用户ID查询指定用户的用户名密码昵称
     *
     */
    /*@GetMapping("select/{id}")
    public JsonResult select(@PathVariable Integer id){
        UserVO userVO = new UserVO();
        if (id){

        }
        return JsonResult.ok(userVO);
    }*/
}
